{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C library to perform Input/Output operations\n",
    "\n",
    "* Change the current working directory into `./demo`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "J:\\SEU\\SEE\\PySEE\\home\\notebook\\demo\n"
     ]
    }
   ],
   "source": [
    "%cd demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'F:\\\\SEU\\\\SEE\\\\PySEE\\\\home\\\\notebook\\\\demo'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## stdio.h - scanf,printf\n",
    "\n",
    "**stdio.h** the header of `C Standard Input and Output Library`\n",
    "\n",
    "### scanf,\n",
    "```c\n",
    "int scanf ( const char * format, ... );\n",
    "```\n",
    "\n",
    "Reads data from  Standard Input(`stdin`) and stores them according to the `parameter format` into the **locations pointed** by the additional arguments.\n",
    "\n",
    "### printf\n",
    "\n",
    "```c\n",
    "int printf ( const char * format, ... );\n",
    "```\n",
    "`Print` **formatted** data to `stdout`\n",
    "\n",
    "Writes the C string pointed by format to the standard output(`stdout`). \n",
    "\n",
    "### parameter format\n",
    "\n",
    "**parameter** \n",
    "\n",
    "* **s**\tString of characters\n",
    "* **d**\tDecimal integer\n",
    "* **f** Floating point number\n",
    "\n",
    "If format includes **format specifiers** (subsequences beginning with `%`), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.\n",
    "\n",
    "Example:\n",
    "```c\n",
    "scanf (\"%79s\",name);\n",
    "```\n",
    "\n",
    "```c\n",
    "printf (\"Mr. %s , %d years old.\\n\",name,age);\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ./src/demostdio.c\n"
     ]
    }
   ],
   "source": [
    "%%file ./src/demostdio.c\n",
    "/*\n",
    "gcc -o demostdio demostdio.c\n",
    "*/\n",
    "#include <stdio.h>\n",
    "\n",
    "int main ()\n",
    "{\n",
    "  char name[80];\n",
    "  int age;\n",
    "  float num;  \n",
    "\n",
    "  printf (\"Enter your family name: \");\n",
    "  scanf (\"%79s\",name);  \n",
    "  \n",
    "  printf (\"Enter your age: \");\n",
    "  scanf (\"%d\",&age);\n",
    "  printf (\"Mr. %s , %d years old.\\n\",name,age);\n",
    "    \n",
    "  printf(\"Enter a number: \");\n",
    "  scanf(\"%f\", &num);  \n",
    "  printf (\"You have entered %f\\n\",num); \n",
    "    \n",
    "  return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "!gcc -o ./bin/demostdio  ./src/demostdio.c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Run in the terminal of ./demo**\n",
    "\n",
    "```\n",
    ">.\\bin\\demostdio\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `*` AND `&` Operator\n",
    "\n",
    "###  The pointer to an variable `*`\n",
    "\n",
    "`*`  at the start of the variable name indicates that this is not declaring an variable, \n",
    "\n",
    "* but `a pointer to an variable`\n",
    "\n",
    "\n",
    "### The Reference operator  `&`\n",
    "\n",
    "**&** is commonly called the `reference`(引用) operator.\n",
    "\n",
    "* `&`： before a variable name means\n",
    "    * address of the variable\n",
    "    \n",
    "    \n",
    " `&a` means `the address` in memory of the variable `a`.\n",
    "\n",
    "\n",
    "\n",
    "You must have seen this notation `&` while using **scanf()** function.\n",
    "\n",
    "```c\n",
    "int age;\n",
    "scanf (\"%d\",&age);\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing ./src/demoref.c\n"
     ]
    }
   ],
   "source": [
    "%%file ./src/demoref.c\n",
    "\n",
    "/*  \n",
    "gcc -o demoref demoref.c\n",
    "*/\n",
    "#include <stdio.h>\n",
    "int main()\n",
    "{\n",
    "  int a;\n",
    "  a = 5;\n",
    "  printf (\"The value of a is %d\\n\", a);\n",
    "  printf (\"The address of a is %d\\n\", &a);\n",
    "  \n",
    "  // ptr_int:the pointer of a integer variable\n",
    "  int *ptr_int;\n",
    "  ptr_int = &a;\n",
    "  printf (\"The value of  ptr_int is %d\\n\", ptr_int);\n",
    "  printf (\"\\tIt stores the value %d\\n\", *ptr_int);\n",
    "  \n",
    "  *ptr_int = 6;\n",
    "  printf (\"It stores the value %d\\n\", *ptr_int);\n",
    "  printf (\"The new value of a is %d\\n\", a);\n",
    "  return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "!gcc -o ./bin/demoref  ./src/demoref.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of a is 5\n",
      "The address of a is 6684180\n",
      "The value of  ptr_int is 6684180\n",
      "\tIt stores the value 5\n",
      "It stores the value 6\n",
      "The new value of a is 6\n"
     ]
    }
   ],
   "source": [
    "!.\\bin\\demoref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
